#include "titlebar.h"
#include "ui_titlebar.h"

TitleBar::TitleBar(QWidget *parent)
    : QWidget{parent}
    , ui(new Ui::TitleBar)
{
    ui->setupUi(this);

    this->m=parent;
    setFixedHeight(75);
    QTimer::singleShot(7000,this,&TitleBar::onPlaceholderText);

    tagbtn=new QPushButton(QIcon(":/icon/"),"",this);
    headshotbtn=new QPushButton(QIcon(":/icon/.png"),"",this);
    mailbtn=new QPushButton(QIcon(":/icon/mail.png"),"",this);
    settingbtn=new QPushButton(QIcon(":/icon/setting.png"),"",this);
    minbtn=new QPushButton(QIcon(":/icon/minimize.png"),"",this);
    maxbtn=new QPushButton(QIcon(":/icon/maximize.png"),"",this);
    windowingbtn=new QPushButton(QIcon(":/icon/windowing.png"),"",this);
    closebtn=new QPushButton(QIcon(":/icon/close.png"),"",this);;
    usernamebtn=new QPushButton(QIcon(""),"",this);
    returnbtn=new QPushButton(QIcon(":/icon/return.png"),"",this);
    voicebtn=new QPushButton(QIcon(":/icon/voice.png"),"",this);
    searchline=new LineEdit(this);
    hlayout=new QHBoxLayout(this);
    frame=new QFrame(this);
    frame->setFrameShape(QFrame::VLine);
    frame->setFixedSize(1,30);
    searchlistw=new SearchListWidget(this);
    searchlistw->hide();
    userinfow=new UserInfoWidget(this);
    userinfow->hide();

    connect(returnbtn,&QPushButton::clicked,this,[this]{emit returnSignal();});
    connect(headshotbtn,&QPushButton::clicked,this,[this]{emit userUiSignal();});
    connect(usernamebtn,&QPushButton::clicked,this,[this]{emit userNameSignal();});
    connect(minbtn,&QPushButton::clicked,this,[this]{emit minSignal();});
    connect(maxbtn,&QPushButton::clicked,this,[this]{emit maxSignal();});
    connect(closebtn,&QPushButton::clicked,this,[this]{emit closeSignal();});

    hlayout->addStretch(1);
    hlayout->addWidget(returnbtn);
    hlayout->addWidget(searchline);
    hlayout->addWidget(voicebtn);
    hlayout->addStretch(1);
    hlayout->addWidget(headshotbtn);
    hlayout->addWidget(usernamebtn);
    hlayout->addWidget(mailbtn);
    hlayout->addWidget(settingbtn);
    hlayout->addWidget(frame);
    hlayout->addWidget(minbtn);
    hlayout->addWidget(maxbtn);
    hlayout->addWidget(closebtn);

    tagbtn->close();

    returnbtn->setFixedHeight(40);
    returnbtn->setFixedWidth(30);
    returnbtn->setStyleSheet( "QPushButton {"
                             "   background-color: #ea3e3e;"  // 背景颜色
                             "   border-radius: 10px;"         // 圆角
                             "   border: 1px solid #f28b8b;"   // 边框颜色
                             "}"
                             "QPushButton:hover {"
                             "   background-color: #f5a8a8;"  // 鼠标悬停时的背景颜色
                             "   border-radius: 10px;"         // 保持圆角
                             "}"
                             );

    searchline->setFixedHeight(40);
    searchline->setFixedWidth(300);
    searchline->setFont(QFont("Microsoft YaHei",11,30));
    searchline->setStyleSheet("QLineEdit {"
                              "   color: #ffffff;"
                              "   background-color: #ea3e3e;"  // 背景颜色
                              "   border-radius: 10px;"         // 圆角
                              "   border: 1px solid #f28b8b;"   // 边框颜色
                              "}"
                              );

    QAction* searchact = new QAction(QIcon(":/icon/search.png"), "", searchline);
    QAction* clearact = new QAction(QIcon(":/icon/clear.png"), "", searchline);
    searchline->searchact=searchact;
    searchline->clearact=clearact;
    searchline->addAction(searchact, QLineEdit::LeadingPosition);
    searchline->addAction(clearact, QLineEdit::TrailingPosition);
    searchline->setFocusPolicy(Qt::ClickFocus);
    searchline->searchlistw=searchlistw;
    connect(searchlistw,&SearchListWidget::searchSignal,this,
            [this](const QString&t){setFocus();searchline->setText(t);emit searchSignal(t);});

    clearact->setVisible(false);
    connect(searchact,&QAction::triggered,this,[this]{
        qDebug()<<"QAction is triggered";
        QString str;
        if(searchline->text().isEmpty())
        {
            str=searchline->toolTip();
        }
        else
        {
            str=searchline->text();
        }
        if(!str.isEmpty())
            emit searchSignal(str);
        this->setFocus();
    });
    connect(clearact,&QAction::triggered,this,[clearact,this]
            {
                searchline->clear();
                clearact->setVisible(false);
                this->searchlistw->turnToNormal();
            });
    connect(searchline,&QLineEdit::textEdited,this,
            [clearact,this]{
                searchlistw->show();
                if (this->searchline->text().isEmpty()) {
                    clearact->setVisible(false);
                    this->searchlistw->turnToNormal();
                }
                else
                {
                    clearact->setVisible(true);
                    this->searchlistw->turnToSearching();
                }
                searchline->setFocus();

            });

    voicebtn->setFixedHeight(40);
    voicebtn->setFixedWidth(40);
    voicebtn->hide();
    voicebtn->setStyleSheet("QPushButton {"
                            "   background-color: #ea3e3e;"  // 背景颜色
                            "   border-radius: 10px;"         // 圆角
                            "   border: 1px solid #f28b8b;"   // 边框颜色
                            "}"
                            "QPushButton:hover {"
                            "   background-color: #f5a8a8;"  // 鼠标悬停时的背景颜色
                            "   border-radius: 10px;"         // 保持圆角
                            "}");

    headshotbtn->setFixedHeight(40);
    headshotbtn->setFixedWidth(40);
    headshotbtn->setStyleSheet("QPushButton {"
                               "   "  // 背景颜色
                               "   border-radius: 20px;"         // 圆角
                               "   border: 1px solid #f28b8b;"   // 边框颜色
                               "}");
    connect(headshotbtn,&QPushButton::clicked,[this]{emit userUiSignal();});

    usernamebtn->setFixedHeight(30);
    usernamebtn->setFixedWidth(100);
    usernamebtn->setStyleSheet("QPushButton {"
                               "   text-align: left;"
                               "   background-color: #ea3e3e;"  // 背景颜色
                               "   border-radius: 10px;"         // 圆角
                               "   color:#fad4d4;"
                               "}"
                               "QPushButton:hover {"
                               "   text-align: left;"
                               "   background-color: #ea3e3e;"  // 背景颜色
                               "   border-radius: 10px;"         // 圆角
                               "   color:#ffffff;"
                               "}"
                               );
    usernamebtn->setFont(QFont("Microsoft YaHei",10));
    usernamebtn->setText("未登录");
    connect(usernamebtn,&QPushButton::clicked,
    [this]
    {
        auto t=mapToGlobal(usernamebtn->pos());
        userinfow->setGeometry(QRect(t.x()-usernamebtn->width(),t.y()+usernamebtn->height(),userinfow->width(),userinfow->height()));
        userinfow->show();
        userinfow->setFocus();
        emit userNameSignal();
    });

    mailbtn->setFixedHeight(40);
    mailbtn->setFixedWidth(40);
    mailbtn->setStyleSheet("QPushButton {"
                           "   background-color: #ea3e3e;"  // 背景颜色
                           "   border:none;"
                           "}");
    mailbtn->setIconSize(QSize(25,25));
    connect(mailbtn,&QPushButton::clicked,[this]{emit mailSignal();});
    mailbtn->hide();//暂时隐藏

    settingbtn->setFixedHeight(40);
    settingbtn->setFixedWidth(40);
    settingbtn->setStyleSheet("QPushButton {"
                              "   background-color: #ea3e3e;"  // 背景颜色
                              "   border:none;"
                              "}");
    settingbtn->setIconSize(QSize(25,25));
    connect(settingbtn,&QPushButton::clicked,[this]{emit settingSignal();});
    settingbtn->hide();//暂时隐藏

    minbtn->setFixedHeight(40);
    minbtn->setFixedWidth(40);
    minbtn->setStyleSheet("QPushButton {"
                          "   background-color: #ea3e3e;"  // 背景颜色
                          "   border:none;"
                          "}");
    minbtn->setIconSize(QSize(20,20));
    connect(minbtn,&QPushButton::clicked,[this]{emit minSignal();});

    maxbtn->setFixedHeight(40);
    maxbtn->setFixedWidth(40);
    maxbtn->setStyleSheet("QPushButton {"
                          "   background-color: #ea3e3e;"  // 背景颜色
                          "   border:none;"
                          "}");
    maxbtn->setIconSize(QSize(20,20));
    connect(maxbtn,&QPushButton::clicked,
            [this]
            {
                hlayout->replaceWidget(maxbtn,windowingbtn);
                windowingbtn->show();
                maxbtn->hide();
                emit maxSignal();
            });

    windowingbtn->setFixedHeight(40);
    windowingbtn->setFixedWidth(40);
    windowingbtn->setStyleSheet("QPushButton {"
                                "   background-color: #ea3e3e;"  // 背景颜色
                                "   border:none;"
                                "}");
    windowingbtn->setIconSize(QSize(20,20));
    windowingbtn->hide();
    connect(windowingbtn,&QPushButton::clicked,
            [this]
            {
                windowingbtn->hide();
                hlayout->replaceWidget(windowingbtn,maxbtn);
                maxbtn->show();
                emit windowingSignal();
            });

    closebtn->setFixedHeight(40);
    closebtn->setFixedWidth(40);
    closebtn->setStyleSheet("QPushButton {"
                            "   background-color: #ea3e3e;"  // 背景颜色
                            "   border:none;"
                            "}");
    closebtn->setIconSize(QSize(20,20));
    connect(closebtn,&QPushButton::clicked,[this]{emit closeSignal();});

    connect(userinfow,&UserInfoWidget::clickDynamic,[this]{emit dynamicSignal();});
    connect(userinfow,&UserInfoWidget::clickConcern,[this]{emit concernSignal();});
    connect(userinfow,&UserInfoWidget::clickFollow,[this]{emit followSignal();});
    connect(userinfow,&UserInfoWidget::clickUserInfo,[this]{emit userInfoSignal();});
    connect(userinfow,&UserInfoWidget::clickUserExit,[this]{emit userExitSignal();});

    //////


}

TitleBar::~TitleBar()
{
    delete ui;
}

void TitleBar::SetPlaceholderText(const QStringList& str)
{
    strlist=str;
    timer=new QTimer(this);
    timer->start(5000);
    connect(timer,&QTimer::timeout,this,&TitleBar::onTimer);
}

void TitleBar::SetDynamicNum(qint32 num)
{
    userinfow->SetDynamicNum(num);
}

void TitleBar::SetConcornNum(qint32 num)
{
    userinfow->SetConcornNum(num);
}

void TitleBar::SetFollowNum(qint32 num)
{
    userinfow->SetFollowNum(num);
}

void TitleBar::onMainWindowResize(QSize size)
{
    this->resize(size.width(),this->height());
}

void TitleBar::onPlaceholderText()
{
    if(!strlist.empty())
    {
        searchline->setPlaceholderText(strlist[pos]);
        pos=(pos+1)%strlist.size();
    }
}

void TitleBar::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e)
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QColor("#ea3e3e")); // 背景颜色
    painter.setPen(Qt::NoPen); // 无边框
    painter.drawRoundedRect(rect(), 10, 10);
    // 覆盖下面两个角为方形(这里rect().center().y()+1是为了能覆盖住下面的边框，不加1无法覆盖会有曲线出现)
    QRect bottomRect(rect().left(), rect().center().y()+1, rect().width(), rect().height()/2);
    painter.drawRect(bottomRect);
}

void TitleBar::mousePressEvent(QMouseEvent*e)
{
    QWidget::mousePressEvent(e);
    setFocus();
    if(e->button()==Qt::LeftButton)
    {
        isdrag=true;
        startpoint=e->globalPosition();
    }
}

void TitleBar::mouseReleaseEvent(QMouseEvent*e)
{
    if (e->button() == Qt::LeftButton)
    {
        isdrag = false;
    }
}

void TitleBar::mouseMoveEvent(QMouseEvent*e)
{

    if(isdrag)
    {
        QPointF movepoint=e->globalPosition();
        QPointF distance=movepoint-startpoint;
        QPoint mpoint=m->pos();
        m->move(mpoint+distance.toPoint());
        startpoint=movepoint;
    }

}

void TitleBar::keyPressEvent(QKeyEvent*e)
{
    if(e->key()==Qt::Key_Enter||e->key()==Qt::Key_Return)
    {
        qDebug()<<"Enter is pressed";
        QString str;
        if(searchline->text().isEmpty())
        {
            str=searchline->toolTip();
        }
        else
        {
            str=searchline->text();
        }
        if(!str.isEmpty())
        {
            setFocus();
            searchlistw->addHistory(QStringList(str));
            searchlistw->UpDataHistoryFront();
            emit searchSignal(str);
        }
    }
}

void TitleBar::onTimer()
{
    strlistpos=(strlistpos+1)%strlist.size();
    searchline->setPlaceholderText(strlist[strlistpos]);
}

void TitleBar::LineEdit::focusInEvent(QFocusEvent *e)
{
    QLineEdit::focusInEvent(e);
    if(searchlistw!=nullptr)
    {
        auto t=mapToGlobal(this->pos());
        searchlistw->setGeometry(QRect(t.x()-this->width(),t.y()+this->height(),searchlistw->width(),searchlistw->height()));//
        searchlistw->show();
    }


}
void TitleBar::LineEdit::focusOutEvent(QFocusEvent *e)
{
    QLineEdit::focusOutEvent(e);
    if(searchlistw!=nullptr)
    {
        searchlistw->hide();
    }
}


/*以下为SearchListWidget*/
/***********************************************************************************************************************/

SearchListWidget::SearchListWidget(QWidget*parent):QWidget(parent)
{
    area=new QScrollArea(this);
    normalw=new QWidget;
    searchingw=new QWidget;
    mainlayout1=new QVBoxLayout(normalw);
    mainlayout2=new QVBoxLayout(searchingw);
    hlayout=new QHBoxLayout;
    searchhistory=new QLabel("搜索历史");
    hotsearch=new QLabel("热搜榜");
    guesssearch=new QLabel("猜你想搜");
    deletebtn=new QPushButton;

    searchhistory->setFont(QFont("Microsoft YaHei",10));
    hotsearch->setFont(QFont("Microsoft YaHei",10));
    guesssearch->setFont(QFont("Microsoft YaHei",10));

    area->setWindowFlags(Qt::Window|Qt::FramelessWindowHint|Qt::Tool);
    area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    area->setStyleSheet("QScrollArea {"
                        "   border-radius: 10px;"
                        "   background-color: #ffffff;"
                        "   border: none;"
                        "}");
    area->setAttribute(Qt::WA_TranslucentBackground);
    area->setWidgetResizable(true);
    area->setFixedSize(400,600);
    area->setWidget(normalw);

    normalw->setStyleSheet(
                           "QWidget {"
                           "   border-radius: 10px;"
                           "   background-color: #ffffff;"
                           "}");
    searchingw->setStyleSheet(normalw->styleSheet());

    //初始化normal模式
    hlayout->addWidget(searchhistory,0,Qt::AlignLeft);
    hlayout->addWidget(deletebtn,0,Qt::AlignRight);
    deletebtn->setIcon(QIcon(":/icon/delete.png"));
    connect(deletebtn,&QPushButton::clicked,this,&SearchListWidget::clearHistory);

    mainlayout1->addLayout(hlayout);
    mainlayout1->addWidget(hotsearch);
    mainlayout1->addStretch(1);

    //初始化searching模式
    mainlayout2->addWidget(guesssearch);
    mainlayout2->addStretch(1);

    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::FramelessWindowHint);
}

void SearchListWidget::readdHistory(AdaptiveLabel*label)
{
    int pos=0;
    while(pos<hlayoutsinsert.size()&&hlayoutsinsert[pos]->indexOf(label)==-1)
        pos++;
    if(pos<hlayoutsinsert.size())
    {
        hlayoutsinsert[pos]->removeWidget(label);
        while(pos>0)
        {
            auto last=static_cast<AdaptiveLabel*>(hlayoutsinsert[pos-1]->itemAt(hlayoutsinsert[pos-1]->count()-1)->widget());//获取最后一个Layout项
            hlayoutsinsert[pos-1]->removeWidget(last);
            hlayoutsinsert[pos]->insertWidget(0,last);
            pos--;
        }
        hlayoutsinsert[0]->insertWidget(0,label);
    }
}

void SearchListWidget::UpDataHistoryFront()
{
    qint16 insertpos;
    insertpos=mainlayout1->indexOf(hlayout)+1;
    while(historyready.size()!=0)
    {
        auto t=historyready.back();
        historyready.pop_back();
        if(!hlayoutsinsert.isEmpty()&&hlayoutsinsert.front()->count()<3)
        {
            hlayoutsinsert.front()->insertWidget(0,t);
            historyinsert.push_front(t);
        }
        else
        {
            auto newhlayout=new QHBoxLayout;
            newhlayout->addWidget(t);
            hlayoutsinsert.push_front(newhlayout);
            historyinsert.push_front(t);
            mainlayout1->insertLayout(insertpos,newhlayout);
        }
    }
    //对排列进行整理
    for(int i=0;i<hlayoutsinsert.size()-1;i++)
    {
        auto next=hlayoutsinsert[i+1];
        while(hlayoutsinsert[i]->count()<3)
        {
            //下面还有就偷过来
            if(next->count()!=0)
            {
                auto w=next->itemAt(0)->widget();
                next->removeWidget(w);
                hlayoutsinsert[i]->addWidget(w);
            }
            //下面没有就结束
            else
            {
                hlayoutsinsert.pop_back();
                mainlayout1->removeItem(next);
                delete next;
                break;
            }
        }
    }

}

void SearchListWidget::UpDataHistoryBack()
{
    qint16 insertpos;
    insertpos=mainlayout1->indexOf(hlayout);
    while(historyready.size()!=0)
    {
        auto t=historyready.front();
        historyready.pop_front();
        if(!hlayoutsinsert.isEmpty()&&hlayoutsinsert.back()->count()<3)
        {
            hlayoutsinsert.front()->insertWidget(hlayoutsinsert.back()->count()-1,t);
        }
        else
        {
            auto newhlayout=new QHBoxLayout;
            newhlayout->addWidget(t);
            hlayoutsinsert.push_back(newhlayout);
            mainlayout1->insertLayout(++insertpos,newhlayout);
        }
    }
}


void SearchListWidget::UpDataHot()
{
    qint16 insertpos;
    if(hotsinsert.empty())
    {
        insertpos=mainlayout1->indexOf(hotsearch);
    }
    else
    {
        insertpos=mainlayout1->indexOf(hotsinsert.back());
    }
    while(hotsready.size()!=0)
    {
        auto t=hotsready.front();
        hotsready.pop_front();
        mainlayout1->insertLayout(++insertpos,t);
        hotsinsert.push_back(t);
    }
}

void SearchListWidget::UpDataSearch()
{
    qint16 insertpos;
    if(searchsinsert.empty())
    {
        insertpos=mainlayout2->indexOf(guesssearch);
    }
    else
    {
        insertpos=mainlayout2->indexOf(searchsinsert.back());
    }
    while(searchsready.size()!=0)
    {
        auto t=searchsready.front();
        searchsready.pop_front();
        mainlayout2->insertWidget(++insertpos,t);
        searchsinsert.push_back(t);
    }
}

void SearchListWidget::addHistory(const QStringList&strlist)
{
    for(auto& elem:strlist)
    {
        bool skip=false;
        AdaptiveLabel*label;
        for(auto&e:historyinsert)
        {
            if(e->text()==elem)
            {
                skip=true;
                label=e;
                break;
            }
        }
        if(skip)
        {
            readdHistory(label);
            continue;
        }
        AdaptiveLabel*ptr=new AdaptiveLabel(this);
        ptr->setFont(QFont("Microsoft YaHei"));
        connect(ptr,&AdaptiveLabel::clicked,this,[this](const QString &t){emit searchSignal(t);});
        connect(ptr,&AdaptiveLabel::clicked,this,[ptr,this]{this->readdHistory(ptr);});
        ptr->setAdaptiveText(elem);
        historyready.push_back(ptr);
    }
}


void SearchListWidget::addHot(const QStringList&strlist)
{
    for(int i=0;i<strlist.size();i++)
    {
        auto layoutptr=new QHBoxLayout;
        auto labelptr=new QLabel;
        auto buttonptr=new QPushButton;
        labelptr->setFont(QFont("Microsoft YaHei"));
        labelptr->setNum(i+1);
        setHotStyle(buttonptr);
        buttonptr->setText(strlist[i]);
        layoutptr->addWidget(labelptr,1);
        layoutptr->addWidget(buttonptr,7);
        layoutptr->addStretch(1);
        hotsready.push_back(layoutptr);
        connect(buttonptr,&QPushButton::clicked,this,
                [buttonptr,this]{addHistory(QStringList(buttonptr->text()));UpDataHistoryFront();emit searchSignal(buttonptr->text());});
    }
}

void SearchListWidget::addSearch(const QStringList&strlist)
{
    for(auto& elem:strlist)
    {
        auto buttonptr=new QPushButton;
        setHotStyle(buttonptr);
        buttonptr->setFixedSize(350,40);
        buttonptr->setText(elem);
        searchsready.push_back(buttonptr);
        connect(buttonptr,&QPushButton::clicked,this,
                [buttonptr,this]{addHistory(QStringList(buttonptr->text()));UpDataHistoryFront();emit searchSignal(buttonptr->text());});
    }
}

QPushButton* SearchListWidget::setHotStyle(QPushButton*button)
{
    button->setFont(QFont("Microsoft YaHei",10,30));
    button->setFixedSize(300,50);
    button->setStyleSheet("QPushButton"
                          "{"
                          "   text-align: left;"
                          "   background-color: #ffffff;"  // 背景颜色
                          "   border-radius: 10px;"         // 圆角
                          "   padding: 10px;"
                          "   border: none;"
                          "}"
                          "QPushButton:hover"
                          "{"
                          "   background-color: #f0f3f6;"
                          "}");
    return button;
}

void SearchListWidget::clearHistory()
{
    for(auto i=hlayoutsinsert.size();i>0;i--)
    {
        mainlayout1->removeItem(hlayoutsinsert.head());
        delete hlayoutsinsert.head();
        hlayoutsinsert.pop_front();
    }
    for(;historyinsert.size()!=0;)
    {
        delete historyinsert.front();
        historyinsert.pop_front();
    }
}

void SearchListWidget::clearSearch()
{
    for(int i=searchsinsert.size()-1;i>=0;i--)
    {
        auto t=searchsinsert[i];
        mainlayout2->removeWidget(t);
        delete t;
        searchsinsert.pop_back();
    }
}

void SearchListWidget::turnToNormal()
{
    issearching=false;
    area->takeWidget();
    area->setWidget(normalw);
    normalw->setStyleSheet(
        "QWidget {"
        "   border-radius: 10px;"
        "   background-color: #ffffff;"
        "}");
}

void SearchListWidget::turnToSearching()
{
    issearching=true;
    area->takeWidget();
    area->setWidget(searchingw);
    searchingw->setStyleSheet(
        "QWidget {"
        "   border-radius: 10px;"
        "   background-color: #ffffff;"
        "}");
}

/*************************************************************************************************************/

AdaptiveLabel::AdaptiveLabel(QWidget*parent):ClickedLabel(parent)
{
    setFont(QFont("Microsoft YaHei",10,30));
    setFixedWidth(120); // 设置固定宽度
    setFixedHeight(30);
    setWordWrap(false); // 禁用自动换行
    setTextFormat(Qt::PlainText); // 设置文本格式为纯文本
    setAlignment(Qt::AlignCenter | Qt::AlignVCenter); // 左对齐
    setStyleSheet( "QLabel {"
                  "   background-color: #f0f3f6;"  // 背景颜色
                  "   border-radius: 15px;"        // 圆角
                  "   padding-left: 10px;"
                  "   padding-right: 10px;"
                  "}"
                  "QLabel:hover {"
                  "   background-color: #e4e8ec;"  // 鼠标悬停时的背景颜色
                  "   border-radius: 15px;"         // 保持圆角
                  "   padding-left: 10px;"
                  "   padding-right: 10px;"
                  "}");
}

void AdaptiveLabel::setAdaptiveText(const QString&text)
{
    realtext=text;
    QFontMetrics metrics(font());
    QString elidedText = metrics.elidedText(text, Qt::ElideRight, width()-20);
    QLabel::setText(elidedText);
}

const QString AdaptiveLabel::adaptiveText()const
{
    return QLabel::text();
}

const QString AdaptiveLabel::text()const
{
    return realtext;
}

void AdaptiveLabel::mousePressEvent(QMouseEvent*e)
{
    if(e->button()==Qt::LeftButton)
    {
        emit clicked(realtext);
    }
}

/*以下为LoginWidget*/
/*******************************************************************************************************************************/

UserInfoWidget::UserInfoWidget(QWidget*parent):QWidget(parent)
{
    hlayout=new QHBoxLayout;
    vlayout=new QVBoxLayout(this);

    for(int i=0;i<3;i++)
    {
        labels.push_back(new MyLabel(this));
        hlayout->addWidget(labels[i]);
    }
    SetDynamicNum(0);
    SetConcornNum(0);
    SetFollowNum(0);

    for(int i=0;i<2;i++)
    {
        auto ptr=new QFrame;
        ptr->setFrameShape(QFrame::VLine);
        ptr->setFixedSize(10,20);
        if(i==0)
            hlayout->insertWidget(1,ptr);
        else
            hlayout->insertWidget(3,ptr);
    }

    vlayout->addLayout(hlayout);

    for(int i=0;i<2;i++)
    {
        buttons.push_back(new QPushButton);
        vlayout->addWidget(buttons[i]);
        buttons[i]->setFont(QFont("Microsoft YaHei",12));
        buttons[i]->setStyleSheet( "QPushButton {"
                                  "   text-align: left;"
                                  "   background-color: #ffffff;"
                                  "   border-radius: 10px;"
                                  "   padding: 10px;"
                                  "   border: none;"
                                  "   color:#787f8d;"
                                  "}"
                                  "   QPushButton:hover{"
                                  "   background-color: #e4e8ec;"
                                  "}"
                                  );
    }

    buttons[0]->setText("个人信息设置");
    buttons[0]->setIcon(QIcon(":/icon/setting.png"));
    buttons[1]->setText("退出登录");
    buttons[1]->setIcon(QIcon(":/icon/exit.png"));

    setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
    setFocusPolicy(Qt::ClickFocus);
    resize(300,200);
    connect(labels[0],&MyLabel::clickLabel,this,[this]{emit clickDynamic();});
    connect(labels[1],&MyLabel::clickLabel,this,[this]{emit clickConcern();});
    connect(labels[2],&MyLabel::clickLabel,this,[this]{emit clickFollow();});
}

void UserInfoWidget::SetDynamicNum(qint32 num)
{
    labels[0]->setText(num,"动态");
}

void UserInfoWidget::SetConcornNum(qint32 num)
{
    labels[1]->setText(num,"关注");
}

void UserInfoWidget::SetFollowNum(qint32 num)
{
    labels[2]->setText(num,"粉丝");
}

void MyLabel::mousePressEvent(QMouseEvent*e)
{
    if(e->button()==Qt::LeftButton)
    {
        emit clickLabel();
    }
}

void MyLabel::setText(qint32 num,const QString&t)
{
    QString newText = QString("<p style='font-size: 20px; text-align: center;'>"
                              "<b>%1</b><br>"
                              "%2"
                              "</p>").arg(QString::number(num)).arg(t);
    QLabel::setText(newText);
}

void UserInfoWidget::paintEvent(QPaintEvent*e)
{
    Q_UNUSED(e)
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QColor("#ffffff"));
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(rect(), 10, 10);
}

void UserInfoWidget::focusOutEvent(QFocusEvent*e)
{
    QWidget::focusOutEvent(e);
    this->hide();
}
